package com.qf.fmall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmall.index.service.IProductImgService;
import com.qf.fmall.index.service.IProductService;
import com.qf.fmall.product.service.*;
import com.qf.fmall.vo.PageVo;
import com.qf.fmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author weijy
 * @since 2024-01-16
 */
@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductSkuService productSkuService;

    @Autowired
    private IProductImgService productImgService;

    @Autowired
    private IProductParamsService productParamsService;

    @Autowired
    private IProductCommentsService productCommentsService;


    @RequestMapping("/detail-info/{productId}")
    public ResultVo detailInfo(@PathVariable("productId") Integer productId) {

        HashMap<String, Object> data = new HashMap<>();

        data.put("product", productService.getProductById(productId));
        data.put("productSkus", productSkuService.getProductById(productId));
        data.put("productImgs", productImgService.getProductById(productId));

        if (data == null) {
            return new ResultVo(-10000, "error", null);
        } else {
            return new ResultVo(10000, "success", data);
        }
    }

    @RequestMapping("/detail-params/{productId}")
    public ResultVo detailParams(@PathVariable("productId") Integer productId) {

        Object param = productParamsService.getParamById(productId);

        if (param == null) {
            return new ResultVo(-10000, "error", null);
        } else {
            return new ResultVo(10000, "success", param);
        }
    }

    @RequestMapping("/detail-commonts/{productId}")
    public ResultVo detailCommonts(@PathVariable Integer productId, Integer pageNum, Integer limit) {

        PageVo pageVo = productCommentsService.getCommentsById(productId, pageNum, limit);
        return new ResultVo(10000,"success",pageVo);
    }


    @RequestMapping("/detail-commontscount/{productId}")
    public ResultVo detailCommontscount(@PathVariable Integer productId) {

        ResultVo resultVo = productCommentsService.getCommentsCount(productId);

        return resultVo;

    }


}
